local M = {
  "nvim-treesitter/nvim-treesitter",
  enabled = true,
  event = { "BufReadPost", "BufNewFile" },
  build = ":TSUpdate",
  dependencies = {
    -- 增强参数高亮
    "m-demare/hlargs.nvim",
    -- 更好的快捷操作， ciw caw vaw va{ da{
    "nvim-treesitter/nvim-treesitter-textobjects",
  },
}

function M.config()
  local status, treesitter = pcall(require, "nvim-treesitter.configs")
  if not status then
    vim.notify "Not found 'nvim-treesitter' plugin"
    return
  end

  -- require('hlargs').enable()
  -- require('hlargs').disable()
  -- require('hlargs').toggle()
  require("hlargs").setup()

  local ensure_installed = {
    "rust",
    "toml",
    "json",
    "markdown",
    "markdown_inline",
    "lua",
    "http",
    "regex",
    "bash",
    "vim",
    "vimdoc",
    "yaml",
    "javascript",
    "typescript",
    "tsx",
    "html",
    "css",
    "scss",
    "vue",
    "python",
    "dockerfile",
    "go",
    "gomod",
    "c",
    "cmake",
    "make",
    "cpp",
    "comment",
    "diff",
    "git_config",
    "git_rebase",
    "gitattributes",
    "gitcommit",
    "gitignore",
    "meson",
    "sql",
    "zig",
    "v",
    "graphql",
    "norg", -- note tool
  }

  treesitter.setup {
    -- 安装 language parser
    -- :TSInstallInfo 命令查看支持的语言
    ensure_installed = ensure_installed,
    ignore_install = {},
    sync_install = false, -- 不同步安装
    auto_install = true,
    highlight = {
      enable = true,
      additional_vim_regex_highlighting = true,
      -- NOTE: these are the names of the parsers and not the filetype. (for example if you want to
      -- disable highlighting for the `tex` filetype, you need to include `latex` in this list as this is
      -- the name of the parser)
      -- list of language that will be disabled
      -- disable = { "c", "rust" },
      -- Or use a function for more flexibility, e.g. to disable slow treesitter highlight for large files
      disable = function(lang, buf)
        local max_filesize = 100 * 1024 -- 100 KB
        local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
        if ok and stats and stats.size > max_filesize then
          return true
        end
      end,
    },
    query_linter = {
      enable = true,
      use_virtual_text = true,
      lint_events = { "BufWrite", "CursorHold" },
    },
    -- 用基于Treesitter的代码格式化, 用 = 格式化代码
    -- gg=G 命令格式化整个文件， 相当于 ggvG= 选中整个文件然后 = 格式化
    -- map("n", "<leader>i", "gg=G", opt)
    indent = {
      enable = true,
      disable = { "yaml" },
    },
    -- 区域选择 -- 启用增量选择
    incremental_selection = {
      enable = true,
      keymaps = {
        init_selection = "<CR>", -- "gnn",
        node_incremental = "<CR>", -- "grn",
        scope_incremental = "<BS>", -- "<SPACE>", -- "grc",
        node_decremental = "<TAB>", -- "grm",
        -- init_selection = "<C-space>",
        -- node_incremental = "<C-space>",
        -- scope_incremental = false,
        -- node_decremental = "<bs>",
      },
    },

    -- 依赖插件 'nvim-treesitter/nvim-treesitter-textobjects'
    textobjects = {
      select = {
        enable = true,
        -- Automatically jump forward to textobj, similar to targets.vim
        lookahead = true,
        -- vif dif  f 函数
        -- diw viw  w 单词
        -- di" vi"  " 一些符号,例如, { ' " [
        keymaps = {
          ["af"] = "@function.outer",
          ["if"] = "@function.inner",
          ["aa"] = "@parameter.outer",
          ["ia"] = "@parameter.inner",
          ["ac"] = "@class.outer",
          -- ['ic'] = '@class.inner',
          ["ic"] = { query = "@class.inner", desc = "Select inner part of a class region" },
          ["as"] = { query = "@scope", query_group = "locals", desc = "Select language scope" },
        },
        selection_modes = {
          ["@parameter.outer"] = "v", -- charwise
          ["@function.outer"] = "V", -- linewise
          ["@class.outer"] = "<c-v>", -- blockwise
        },
        include_surrounding_whitespace = false,
      },
      swap = {
        -- 调换参数
        enable = true,
        swap_next = {
          ["<A-space>n"] = "@parameter.inner",
        },
        swap_previous = {
          ["<A-space>A"] = "@parameter.inner",
        },
      },
      move = {
        enable = true,
        set_jumps = true, -- whether to set jumps in the jumplist
        goto_next_start = {
          ["]m"] = "@function.outer",
          ["]]"] = "@class.outer",
        },
        goto_next_end = {
          ["]M"] = "@function.outer",
          ["]["] = "@class.outer",
        },
        goto_previous_start = {
          ["[m"] = "@function.outer",
          ["[["] = "@class.outer",
        },
        goto_previous_end = {
          ["[M"] = "@function.outer",
          ["[]"] = "@class.outer",
        },
      },
    },
  }

  vim.keymap.set("n", "<leader>tf", "gg=G", { silent = true, noremap = true })
  --
  -- -- 开启 Folding
  -- vim.wo.foldmethod = "expr"
  -- vim.wo.foldexpr = "nvim_treesitter#foldexpr()"
  -- -- 默认不要折叠
  -- -- https://stackoverflow.com/questions/8316139/how-to-set-the-default-to-unfolded-when-you-open-a-file
  -- vim.wo.foldlevel = 99
  -- -- zc， zo 会 折叠 {} 里的内容
end

return M
